package cn.nebula.music.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.CombinationAlbumDAO;
import cn.nebula.music.dao.CombinationClassificationDAO;
import cn.nebula.music.dao.CombinationDAO;
import cn.nebula.music.dao.CombinationSingerDAO;
import cn.nebula.music.dao.CombinationSongDAO;
import cn.nebula.music.dao.UserCombinationDAO;
import cn.nebula.music.domain.Album;
import cn.nebula.music.domain.Classification;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.CombinationAlbum;
import cn.nebula.music.domain.CombinationClassification;
import cn.nebula.music.domain.CombinationSinger;
import cn.nebula.music.domain.CombinationSong;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.User;
import cn.nebula.music.domain.UserCombination;

@Service
public class CombinationServiceImpl implements CombinationService {

	@Autowired
	private CombinationDAO combinationDAO;

	@Autowired
	private CombinationSingerDAO combinationSingerDAO;

	@Autowired
	private CombinationClassificationDAO combinationClassificationDAO;

	@Autowired
	private UserCombinationDAO userCombinationDAO;

	@Autowired
	private CombinationAlbumDAO combinationAlbumDAO;

	@Autowired
	private CombinationSongDAO combinationSongDAO;

	@Override
	public Set<Combination> findAllCombinations() throws Exception {
		return combinationDAO.findAllCombinations();
	}

	@Override
	public Set<Combination> findAllCombinationsOfBT() throws Exception {
		return combinationDAO.findAllCombinationsOfBT();
	}

	@Override
	public Set<Combination> findAllCombinationsWP() throws Exception {
		return combinationDAO.findAllCombinationsWP();
	}

	@Override
	public Combination findCombinationById(Integer id) throws Exception {
		return combinationDAO.findCombinationById(id);
	}

	@Override
	public Combination findCombinationByName(String name) throws Exception {
		return combinationDAO.findCombinationByName(name);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addCombination(Combination combination) throws Exception {
		combinationDAO.addCombination(combination);
		Set<Singer> singers = combination.getSingers();
		if (singers != null) {
			for (Singer singer : singers) {
				CombinationSinger cs = new CombinationSinger();
				cs.setCombination(combination.getId());
				cs.setSinger(singer.getId());
				combinationSingerDAO.addCombinationSinger(cs);
			}
		}
		Set<Classification> classifications = combination.getClassifications();
		if (classifications != null) {
			for (Classification classification : classifications) {
				CombinationClassification cc = new CombinationClassification();
				cc.setCombination(combination.getId());
				cc.setClassification(classification.getId());
				combinationClassificationDAO.addCombinationClassification(cc);
			}
		}
		Set<User> users = combination.getUsers();
		if (users != null) {
			for (User user : users) {
				UserCombination uc = new UserCombination();
				uc.setCombination(combination.getId());
				uc.setUser(user.getId());
				userCombinationDAO.addUserCombination(uc);
			}
		}
		Set<Album> albums = combination.getAlbums();
		if (albums != null) {
			for (Album album : albums) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setCombination(combination.getId());
				ca.setAlbum(album.getId());
				combinationAlbumDAO.addCombinationAlbum(ca);
			}
		}
		Set<Song> songs = combination.getSongs();
		if (songs != null) {
			for (Song song : songs) {
				CombinationSong cs = new CombinationSong();
				cs.setCombination(combination.getId());
				cs.setSong(song.getId());
				combinationSongDAO.addCombinationSong(cs);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateCombination(Combination combination) throws Exception {
		combinationDAO.updateCombination(combination);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<User> users1 = findCombinationById(combination.getId()).getUsers();
		Set<User> users2 = combination.getUsers();
		if (users1 != null) {
			size1 = users1.size();
		}
		if (users2 != null) {
			size2 = users2.size();
		}
		if (size1 == size2) {
			if (!users1.toString().equals(users2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (User user : users1) {
				UserCombination uc = new UserCombination();
				uc.setCombination(combination.getId());
				uc.setUser(user.getId());
				userCombinationDAO.deleteUserCombination(uc);
			}
			for (User user : users2) {
				UserCombination uc = new UserCombination();
				uc.setCombination(combination.getId());
				uc.setUser(user.getId());
				userCombinationDAO.addUserCombination(uc);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Classification> classifications1 = findCombinationById(combination.getId()).getClassifications();
		Set<Classification> classifications2 = combination.getClassifications();
		if (classifications1 != null) {
			size1 = classifications1.size();
		}
		if (classifications2 != null) {
			size2 = classifications2.size();
		}
		if (size1 == size2) {
			if (!classifications1.toString().equals(classifications2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Classification classification : classifications1) {
				CombinationClassification cc = new CombinationClassification();
				cc.setCombination(combination.getId());
				cc.setClassification(classification.getId());
				combinationClassificationDAO.deleteCombinationClassification(cc);
			}
			for (Classification classification : classifications2) {
				CombinationClassification cc = new CombinationClassification();
				cc.setCombination(combination.getId());
				cc.setClassification(classification.getId());
				combinationClassificationDAO.addCombinationClassification(cc);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Singer> singers1 = findCombinationById(combination.getId()).getSingers();
		Set<Singer> singers2 = combination.getSingers();
		if (singers1 != null) {
			size1 = singers1.size();
		}
		if (singers2 != null) {
			size2 = singers2.size();
		}
		if (size1 == size2) {
			if (!singers1.toString().equals(singers2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Singer singer : singers1) {
				CombinationSinger cs = new CombinationSinger();
				cs.setCombination(combination.getId());
				cs.setSinger(singer.getId());
				combinationSingerDAO.deleteCombinationSinger(cs);
			}
			for (Singer singer : singers2) {
				CombinationSinger cs = new CombinationSinger();
				cs.setCombination(combination.getId());
				cs.setSinger(singer.getId());
				combinationSingerDAO.addCombinationSinger(cs);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Album> albums1 = findCombinationById(combination.getId()).getAlbums();
		Set<Album> albums2 = combination.getAlbums();
		if (albums1 != null) {
			size1 = albums1.size();
		}
		if (albums2 != null) {
			size2 = albums2.size();
		}
		if (size1 == size2) {
			if (!albums1.toString().equals(albums2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Album album : albums1) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setCombination(combination.getId());
				ca.setAlbum(album.getId());
				combinationAlbumDAO.deleteCombinationAlbum(ca);
			}
			for (Album album : albums2) {
				CombinationAlbum ca = new CombinationAlbum();
				ca.setCombination(combination.getId());
				ca.setAlbum(album.getId());
				combinationAlbumDAO.addCombinationAlbum(ca);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Song> songs1 = findCombinationById(combination.getId()).getSongs();
		Set<Song> songs2 = combination.getSongs();
		if (songs1 != null) {
			size1 = songs1.size();
		}
		if (songs2 != null) {
			size2 = songs2.size();
		}
		if (size1 == size2) {
			if (!songs1.toString().equals(songs2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Song song : songs1) {
				CombinationSong cs = new CombinationSong();
				cs.setCombination(combination.getId());
				cs.setSong(song.getId());
				combinationSongDAO.deleteCombinationSong(cs);
			}
			for (Song song : songs2) {
				CombinationSong cs = new CombinationSong();
				cs.setCombination(combination.getId());
				cs.setSong(song.getId());
				combinationSongDAO.addCombinationSong(cs);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteCombinationById(Integer id) throws Exception {
		combinationSingerDAO.deleteCombinationSingerByCombinationId(id);
		combinationClassificationDAO.deleteCombinationClassificationByCombinationId(id);
		userCombinationDAO.deleteUserCombinationByCombinationId(id);
		combinationAlbumDAO.deleteCombinationAlbumByCombinationId(id);
		combinationSongDAO.deleteCombinationSongByCombinationId(id);
		combinationDAO.deleteCombinationById(id);
	}

}
